package ratelimit

import (
	"context"
	"errors"
	"gitee.com/xiao_hange/go-admin-pkg/pkg/ratelimit"
	"go-admin/server/internal/service/sms"
)

var ErrRateLimit = errors.New("限流了")

type RateLimitSMSService struct {
	svc     sms.Service
	limiter ratelimit.Limiter
}

func NewRateLimitSMSService(svc sms.Service, limiter ratelimit.Limiter) *RateLimitSMSService {
	return &RateLimitSMSService{
		svc:     svc,
		limiter: limiter,
	}
}

func (r RateLimitSMSService) Send(ctx context.Context, tpl string, args []string, numbers ...string) error {
	limited, err := r.limiter.Limited(ctx, "sms:limit")
	if err != nil {
		return errors.New("限流器出现故障！")
	}
	if limited {
		return ErrRateLimit
	}
	return r.svc.Send(ctx, tpl, args, numbers...)
}
